﻿//
// GNS_PlayerDate.cpp
// MobileManageSys
//FuncTion  直属玩家详情
// Created by mr.l on 16/1/16.
//
//
#include "GnsClientFrame.h"
#include "GNS_PlayerDate.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
#include "GNS_SceneMgr.h"
#include "SystemMgr.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()
//extern ISocketMgr * _GameSock;
GNS_PlayerDate::GNS_PlayerDate()
:_niChenTextField(nullptr)
, _nameTextField(nullptr)
, _phoneTextField(nullptr)
{
    pGNS_PlayerDate=0;
    ReaName=nullptr;
    IphoneNun=nullptr;
    IsInput = false;
    NickNamebg=nullptr;
    pPrivate=nullptr;
    nameTextField=nullptr;
    niChenTextField=nullptr;
    phoneTextField=nullptr;
}

GNS_PlayerDate::~GNS_PlayerDate()
{
   
}

GNS_PlayerDate* GNS_PlayerDate::createLayer()
{
    auto pGNS_PlayerDate = GNS_PlayerDate::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_PlayerDate);
    return pGNS_PlayerDate;
}

bool GNS_PlayerDate::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    return true;
}
void GNS_PlayerDate::SetNodeName(std::string CsbName)
{
    pGNS_PlayerDate = CSLoader::createNode(CsbName);
    pGNS_PlayerDate->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_PlayerDate);
    addChild(pGNS_PlayerDate,11);
    
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_PlayerDate);
	GNS_NodeMgr::getInstance()->pushChildNode("GNS_PlayerDate", pGNS_PlayerDate);

    auto pBackBtn = static_cast<Button*>(pGNS_PlayerDate->getChildByName("PlayerBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PlayerDate::BtnCallBack, this));
    //UI界面
    TitleText = static_cast<Text*>(pGNS_PlayerDate->getChildByName("Text_5"));
    IdText = static_cast<Text*>(pGNS_PlayerDate->getChildByName("TextDate"));
    GlodText= static_cast<Text*>(pGNS_PlayerDate->getChildByName("TextDate_2_0"));
    
    TopUpText = static_cast<Text*>(pGNS_PlayerDate->getChildByName("TextDate_2"));
    TickText = static_cast<Text*>(pGNS_PlayerDate->getChildByName("TextDate_3"));
    StateText = static_cast<Text*>(pGNS_PlayerDate->getChildByName("TextDate_4"));
    InfoText = static_cast<Text*>(pGNS_PlayerDate->getChildByName("TextDate_6"));
    IntoText =static_cast<Text*>(pGNS_PlayerDate->getChildByName("TextDate_7"));
    //输入框
	/*
      RemTextF = GNS_SceneMgr::getInstance()->MycreateEditBox("输入备注名",Vec2(winsize.width*0.52, winsize.height*0.76), 24, Color3B::BLACK, 10, false);
   
    pGNS_PlayerDate->addChild(RemTextF,12);  //****
    
     NickNameTextF = GNS_SceneMgr::getInstance()->MycreateEditBox("输入昵称",Vec2(winsize.width*0.52, winsize.height*0.8055), 24, Color3B::BLACK, 10, false);
     pGNS_PlayerDate->addChild(NickNameTextF,12);
    
     MobilephoneNumTextF = GNS_SceneMgr::getInstance()->MycreateEditBox("输入手机号码",Vec2(winsize.width*0.52, winsize.height*0.4445), 24, Color3B::BLACK, 10, false); //
     pGNS_PlayerDate->addChild(MobilephoneNumTextF,12);
    */
	nameTextField = static_cast<TextField*>(pGNS_PlayerDate->getChildByName("TextField_name"));
	nameTextField->setPlaceHolder(CCGnsOften::getUtf8_DirectAssignmentInProgram("请输入昵称"));
	niChenTextField = static_cast<TextField*>(pGNS_PlayerDate->getChildByName("TextField_nichen"));
	niChenTextField->setPlaceHolder(CCGnsOften::getUtf8_DirectAssignmentInProgram("请输入备注名"));
	phoneTextField = static_cast<TextField*>(pGNS_PlayerDate->getChildByName("TextField_phoneNum"));
	phoneTextField->setPlaceHolder(CCGnsOften::getUtf8_DirectAssignmentInProgram("请输入手机号"));

	_nameTextField = GNS_Handler::createTMEditBoxWithExistFeild(nameTextField, pGNS_PlayerDate, 14);
	_niChenTextField = GNS_Handler::createTMEditBoxWithExistFeild(niChenTextField, pGNS_PlayerDate, 14);
	_phoneTextField = GNS_Handler::createTMEditBoxWithExistFeild(phoneTextField, pGNS_PlayerDate, 20, EditBox::InputMode::NUMERIC);


    pForbid = static_cast<Button*>(pGNS_PlayerDate->getChildByName("Forbid"));
    pForbid->setTag(2);
    pForbid->addTouchEventListener(CC_CALLBACK_2(GNS_PlayerDate::BtnCallBack, this));
    
    auto pRevetPass = static_cast<Button*>(pGNS_PlayerDate->getChildByName("RevetPass"));
    pRevetPass->setTag(3);
    pRevetPass->addTouchEventListener(CC_CALLBACK_2(GNS_PlayerDate::BtnCallBack, this));
    
    pPrivate = static_cast<Button*>(pGNS_PlayerDate->getChildByName("private"));
    if (GNS_PlayerMgr::getInstance()->getUserLevel() == 0)
    {
        pPrivate->setVisible(false);
    }
    else
    {
        pPrivate->setVisible(true);
        pPrivate->setTag(4);
        pPrivate->addTouchEventListener(CC_CALLBACK_2(GNS_PlayerDate::BtnCallBack, this));
    }
    
    ReaName = static_cast<ImageView*>(pGNS_PlayerDate->getChildByName("Image_2_0"));
    ReaName->setVisible(false);
    IphoneNun = static_cast<ImageView*>(pGNS_PlayerDate->getChildByName("Image_2_1"));
    IphoneNun->setVisible(false);
    NickNamebg = static_cast<ImageView*>(pGNS_PlayerDate->getChildByName("Image_2"));
    NickNamebg->setVisible(false);
    
}
void GNS_PlayerDate::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:  // 返回
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.erase(0);
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()->removeChild(this);
                }
				//GNS_NodeMgr::getInstance()->eraseChildNode("GNS_PlayerDate");
                //auto PlayerNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(2);
                //PlayerNode->setVisible(true);

				GNS_NodeMgr::getInstance()->setMenuVisible("Player", true);
                break;
            }
            case 2:
            {
                if (PlayerState==true) {
                    //PlayerState=false;
                    Req_forbid_user pForbid;
                    pForbid.OperatorUserId=GNS_PlayerMgr::getInstance()->getMyUseridx();
                    pForbid.UserId=PlayerID;
                    //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_REQ_FORBID_USER, &pForbid, sizeof(pForbid));
                    GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_REQ_FORBID_USER, (char*)&pForbid, sizeof(pForbid));
					//StateText->setString("封号");
                }else{
                    //PlayerState=true;
                    Req_unforbid_user pUnforbid;
                    pUnforbid.OperatorUserId=GNS_PlayerMgr::getInstance()->getMyUseridx();
                    pUnforbid.UserId=PlayerID;
                    //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_REQ_UNFORBID_USER, &pUnforbid, sizeof(pUnforbid));
                    GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_REQ_UNFORBID_USER, (char*)&pUnforbid, sizeof(pUnforbid));
					//StateText->setString("正常");
                }
                break;
            }
            case 3:  //重置密码
            {
                CMD_CS_ResetLowerLinePlayerPassword pResetPass;
                pResetPass.llUserID=PlayerID;
                pResetPass.llFatherAgentID=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_USER_LOWER_LINE_PLAYER_RESET_PASSWORD, &pResetPass, sizeof(pResetPass));
               GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_USER_LOWER_LINE_PLAYER_RESET_PASSWORD,(char*) &pResetPass, sizeof(pResetPass));
                break;
            }
            case 4:  //修改资料
            {
                if (IsInput==false) {
                    IsInput=true;
                    NickNamebg->setVisible(true);
                    ReaName->setVisible(true);
                    IphoneNun->setVisible(true);
                    pPrivate->loadTextureNormal("change_password_finish.png");
                    pPrivate->loadTexturePressed("change_password_finish.png");   

					_niChenTextField->setEnabled(true);
					_nameTextField->setEnabled(true);
					_phoneTextField->setEnabled(true);
                    
                }else{

					if (!checkInput())
						return;

                    IsInput=false;
                    pPrivate->loadTextureNormal("change_password_button.png");
                    pPrivate->loadTexturePressed("change_password_button.png");
                    NickNamebg->setVisible(false);
                    ReaName->setVisible(false);
                    IphoneNun->setVisible(false);

					_niChenTextField->setEnabled(false);
					_nameTextField->setEnabled(false);
					_phoneTextField->setEnabled(false);
                    
                    CMD_CS_SetUserPrivateInfo pSetUserPri;
                    pSetUserPri._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                    pSetUserPri._playerId=PlayerID;

                    #if 1       //UTF GB2312测试使用
					string tmpnickgb = SystemMgr::getInstance()->utf8ToGb(_nameTextField->getText());
					string tmpremarkgb = SystemMgr::getInstance()->utf8ToGb(_niChenTextField->getText());
					memcpy(pSetUserPri._nickName, tmpnickgb.c_str(), tmpnickgb.length() + 1);
					memcpy(pSetUserPri._remarkName, tmpremarkgb.c_str(), tmpremarkgb.length() + 1);  //RemTextF
                   #else
					memcpy(pSetUserPri._nickName, _nameTextField->getText(), 33);
					memcpy(pSetUserPri._remarkName, _niChenTextField->getText(), 33);  //RemTextF
                   #endif

                    memcpy(pSetUserPri._cellPhone, _phoneTextField->getText(), 20);
                    //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_SET_USER_PRIVATE_INFO, &pSetUserPri, sizeof(pSetUserPri));
                    GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_SET_USER_PRIVATE_INFO, (char*)&pSetUserPri, sizeof(pSetUserPri));
                }
                break;
            }
            default:
                break;
        }
    }
}
void GNS_PlayerDate::ShowPlayerDate(CMD_SC_GetLowlinePlayerInfoResult Getlowlnfo) //显示详细信息
{
    TitleText->setString(StringUtils::format("ID:%d",Getlowlnfo._userId));
    PlayerID=Getlowlnfo._userId;
    IdText->setString(StringUtils::format("%d",Getlowlnfo._userId));
    _niChenTextField->setText(SystemMgr::getInstance()->GbToUTF8(Getlowlnfo._userRemarkName).c_str());
    _nameTextField->setText(SystemMgr::getInstance()->GbToUTF8(Getlowlnfo._userNickName).c_str());  //RemTextF
    GlodText->setString(StringUtils::format("%lld",Getlowlnfo._userGold));
    TopUpText->setString(StringUtils::format("%lld",Getlowlnfo._userRecharge));
    TickText->setString(StringUtils::format("%lld",Getlowlnfo._userReward));
    _phoneTextField->setText(SystemMgr::getInstance()->GbToUTF8(Getlowlnfo._cellPhone).c_str());

	_niChenTextField->setEnabled(false);
	_nameTextField->setEnabled(false);
	_phoneTextField->setEnabled(false);
    if(Getlowlnfo._state==0)
    {
        StateText->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("封号"));
        pForbid->loadTextureNormal("de-archive.png");
		pForbid->loadTexturePressed("de-archive.png");
        PlayerState=false;
    }else{
        StateText->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("正常")); //正常
        pForbid->loadTextureNormal("sethonor.png");
		pForbid->loadTexturePressed("sethonor.png");
        PlayerState=true;
    }
    InfoText->setString(StringUtils::format("%lld",Getlowlnfo._transferIn));
    IntoText->setString(StringUtils::format("%lld",Getlowlnfo._transferOut));
}
void GNS_PlayerDate::SetButtonState(bool state)
{
    if (state == false) {
        pForbid->loadTextureNormal("sethonor.png");
		pForbid->loadTexturePressed("sethonor.png");
        StateText->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("正常"));
    }else{
        pForbid->loadTextureNormal("de-archive.png");
		pForbid->loadTexturePressed("de-archive.png");
        StateText->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("封号"));
    }
	PlayerState = !state;
}

 void GNS_PlayerDate::textFieldEvent(Ref * pSender , ui::TextField::EventType type,int flag)
{
    
}

 bool GNS_PlayerDate::checkInput()
 {
	 string nicheng = _nameTextField->getText();
	 string beizhu = _niChenTextField->getText();
	 string phoneNum = _phoneTextField->getText();

	 if (!CCGnsOften::checkPlayerName(nicheng))
	 {
		 OnUIAction("昵称长度有误！", true);
		 return false;
	 }
	 else if (!CCGnsOften::checkPlayerName(beizhu))
	 {
		 OnUIAction("备注名长度有误！", true);
		 return false;
	 }
	 else if (!CCGnsOften::checkPhoneNum(phoneNum))
	 {
		 OnUIAction("手机号码长度有误！", true);
		 return false;
	 }

	 return true;
 }

 void GNS_PlayerDate::OnUIAction(std::string Text, bool isUTF)
 {
	 auto pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
	 pPopWnd->setClickWndOutsideClose(true);
	 pPopWnd->SetTitleWithUTF("提示!");
	 if (isUTF)
		 pPopWnd->SetContentTextWithUTF(Text, Color3B::BLACK);
	 else
		 pPopWnd->SetContentTextWithGB(Text, Color3B::BLACK);
 }